package com.mlz.mlzmall.ui.activity.act.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.GetProductToExchangeListResponse;
import com.mlz.mlzmall.bean.GetProductToExchangeResponseid;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.AppManager;
import com.mlz.mlzmall.ui.utils.util.ListViewUtils;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * describe :
 * Created by Pm on 2016/5/30.
 * PackageName com.mlz.mlzmall.ui.activity.act.personal.
 * ProjectName mlzmall_android.
 */

public class IntergationRedemptionActivity extends BaseActivity {
    private RelativeLayout rl_back;

    private GetProductToExchangeResponseid.ProductToExchangeDto data;
    private int ProductToExchangeId;
    private TextView tv_title_integration;
    private ImageView iv_integretion_redermption;
    private TextView tv_point_title;
    private TextView tv_piece_mount;
    private TextView tv_price_redermptions;
    private TextView tv_intergation_need;
    private TextView tv_time_first;//初始时间
    private TextView tv_time_last;

    private Button btn_no_enough;//积分不足
    private Button btn_had_do;//已兑换
    private Button btn_over;//已过期
    private Button btn_cxchange_now;//马上兑换

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imtergation_redemption);
        AppManager.getAppManager().addActivity(this);
       // ProductToExchangeId = getIntent().getIntExtra("ProductToExchangeId", 0);
        ProductToExchangeId = Integer.parseInt(String.valueOf(SharedUtils.get(IntergationRedemptionActivity.this,"ProductToExchangeId",0)));
        LogUtils.d("ProductToExchangeId:"+ProductToExchangeId);
        initView();
        initListenter();
        initData();
    }

    private void initData() {
        getData();
    }

    private void initListenter() {
        rl_back.setOnClickListener(this);
    }

    private void initView() {
        rl_back = (RelativeLayout) findViewById(R.id.iv_back_redemption);
        tv_title_integration = (TextView) findViewById(R.id.tv_title_integration);
        iv_integretion_redermption = (ImageView) findViewById(R.id.iv_integretion_redermption);
        tv_piece_mount = (TextView) findViewById(R.id.tv_piece_mounts);
        tv_price_redermptions = (TextView) findViewById(R.id.tv_price_redermptions);
        tv_intergation_need = (TextView) findViewById(R.id.tv_intergation_need);
        tv_point_title = (TextView) findViewById(R.id.tv_point_title);
        tv_time_first = (TextView) findViewById(R.id.tv_time_first);
        tv_time_last = (TextView) findViewById(R.id.tv_time_last);
        btn_no_enough = (Button) findViewById(R.id.btn_no_enough);//积分不足
        btn_had_do = (Button) findViewById(R.id.btn_had_do);//已兑换
        btn_over = (Button) findViewById(R.id.btn_over);//已过期
        btn_cxchange_now = (Button) findViewById(R.id.btn_cxchange_now);//马上兑换
    }

    public void getData() {
        tag = "IntergationRedemptionActivity_getData";
        JSONObject jsonObject = new JSONObject(getMall());
      //  JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzmall.com/api/point/exchange/productToExchangeDetail", jsonObject, new Response.Listener<JSONObject>() {
     //   JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzMall.com/api/point/exchange/productToExchangeDetail?productToExchangeId="+ProductToExchangeId,null, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.EXCHANGE.PRODUCTTOEXCHANGDETAIL+"?productToExchangeId="+ProductToExchangeId,null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                LogUtils.d(jsonObject.toString());
                Gson gson = new Gson();
                GetProductToExchangeResponseid bean = gson.fromJson(jsonObject.toString(), GetProductToExchangeResponseid.class);
                if (bean != null) {
                    data = bean.getData();
                    if (data != null) {
                        //更新UI
                        tv_title_integration.setText(data.getProductName());//商品名字)
                        //imageLoader.displayImage(productPicture, iv_integretion_redermption, options);//商品图片
                        ImageLoader.getInstance().displayImage(data.getImage(), iv_integretion_redermption);
                        tv_piece_mount.setText(data.getQuantity() + "");//商品数量
                        tv_point_title.setText(data.getPointsAmount() + "");//积分数
                        tv_price_redermptions.setText(data.getFreight() + "");
                        tv_intergation_need.setText(data.getDescription());

                        SimpleDateFormat formatEnd = new SimpleDateFormat("yyyy-MM-dd");
                        Date dat_start = new Date(data.getDateStart());
                        java.util.GregorianCalendar ge = new java.util.GregorianCalendar();
                        ge.setTime(dat_start);
                        String dataStart = formatEnd.format(ge.getTime());
                        tv_time_first.setText(dataStart);//初始时间

                        Date dat_end = new Date(data.getDateEnd());
                        java.util.GregorianCalendar end = new java.util.GregorianCalendar();
                        end.setTime(dat_end);
                        String dataEnd = formatEnd.format(end.getTime());
                        tv_time_last.setText(dataEnd);

                        switch (data.getStatus()) {//判断是否可兑换的状态
                            case "EXPIRED":
                                btn_over.setVisibility(View.VISIBLE);
                                break;
                            case "AVAILABLE":
                                btn_cxchange_now.setVisibility(View.VISIBLE);
                                btn_cxchange_now.setOnClickListener(IntergationRedemptionActivity.this);
                                break;
                            case "STOCKOUT":
                                btn_had_do.setVisibility(View.VISIBLE);
                                break;
                            case "INSUFFICIENT_POINT":
                                btn_no_enough.setVisibility(View.VISIBLE);
                                break;
                            default:
                                break;
                        }
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(tag, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                LogUtils.d("sessionId="+sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    private Map<String, String> getMall() {
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("productToExchangeId", "" + ProductToExchangeId);
        return param1;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_redemption:
                finish();
                //          overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.btn_cxchange_now:
                Intent intent2 = new Intent();
                intent2.setClass(IntergationRedemptionActivity.this, ReceiveAddress.class);
                intent2.putExtra(ConfigInfo.ACT_STATUS, "1002");
                startActivity(intent2);
                overridePendingTransition(R.anim.in_from_right_back_center, R.anim.out_to_left_back_center);
           //     Toast.makeText(IntergationRedemptionActivity.this,"dd",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
